#include <iostream>

#include "Databank.hpp"
#include "Game.hpp"
#include "TurnSchedule.hpp"

#include "Player.hpp"

Player* gPlayer;

Player::Player(std::string name, unsigned int race) : Actor(name, race)
{
	Shape = '@';
}

Player::~Player()
{
}

bool Player::takeTurn()
{
	if (Alive)
	{
		unsigned int delay = 0;

		TCOD_key_t key = TCODConsole::waitForKeypress(false);
		if (key.c == 'Q')
			return false;
		else if (key.vk == TCODK_UP || key.vk == TCODK_KP8)
			delay = move(Vector2d(0, -1));
		else if (key.vk == TCODK_LEFT || key.vk == TCODK_KP4)
			delay = move(Vector2d(-1, 0));
		else if (key.vk == TCODK_DOWN || key.vk == TCODK_KP2)
			delay = move(Vector2d(0, 1));
		else if (key.vk == TCODK_RIGHT || key.vk == TCODK_KP6)
			delay = move(Vector2d(1, 0));
		else if (key.vk == TCODK_KP7)
			delay = move(Vector2d(-1, -1));
		else if (key.vk == TCODK_KP9)
			delay = move(Vector2d(1, -1));
		else if (key.vk == TCODK_KP1)
			delay = move(Vector2d(-1, 1));
		else if (key.vk == TCODK_KP3)
			delay = move(Vector2d(1, 1));
		else if (key.c == '.')
			delay = move(Vector2d(0, 0));
		else if (key.c == 'o')
			delay = OpenClose();
		else if (key.c == 'b')
			delay = Burn();

		TurnSchedule::scheduleTurn(this, delay);
	}
	else
	{
		std::cout<<"You have died..."<<std::endl;
		std::cout<<"Press any key..."<<std::endl;
		std::cin.get();
		return false;
	}

	return true;
}

int Player::Burn()
{
	Vector2d pos = Position;
	Game::render('x', pos, Databank::Race[Race].Sight, true);

	while(true)
	{
		TCOD_key_t key = TCODConsole::root->waitForKeypress(true);
		if (key.vk == TCODK_ESCAPE)
			break;
		else if ((key.vk == TCODK_UP || key.vk == TCODK_KP8) && Position.Y-pos.Y < 1)
			--pos.Y;
		else if ((key.vk == TCODK_LEFT || key.vk == TCODK_KP4) && Position.X-pos.X < 1)
			--pos.X;
		else if ((key.vk == TCODK_DOWN || key.vk == TCODK_KP2) && pos.Y-Position.Y < 1)
			++pos.Y;
		else if ((key.vk == TCODK_RIGHT || key.vk == TCODK_KP6) && pos.X-Position.X < 1)
			++pos.X;
		else if (key.vk == TCODK_KP7 && Position.Y-pos.Y < 1 && Position.X-pos.X < 1)		
		{
			--pos.X;
			--pos.Y;
		}
		else if (key.vk == TCODK_KP9 && pos.X-Position.X < 1 && Position.Y-pos.Y < 1)
		{
			++pos.X;
			--pos.Y;
		}
		else if (key.vk == TCODK_KP1 && Position.X-pos.X < 1 && pos.Y-Position.Y < 1)
		{
			--pos.X;
			++pos.Y;
		}
		else if (key.vk == TCODK_KP3 && pos.X-Position.X < 1 && pos.Y-Position.Y < 1)
		{
			++pos.X;
			++pos.Y;
		}
		else if (key.vk == TCODK_ENTER)
		{
			Tile* tile = Area->getTile(pos);
			tile->getItems().back()->onBurn();
			return 1;
		}

		Game::render('x', pos, Databank::Race[Race].Sight, true);
	}

	Game::render(Shape, Position, Databank::Race[Race].Sight, false);

	return 0;
}

int Player::OpenClose()
{
	Tile* tiles[3][3];
	tiles[0][0] = Area->getTile(Position+Vector2d(-1,-1));
	tiles[1][0] = Area->getTile(Position+Vector2d(0, -1));
	tiles[2][0] = Area->getTile(Position+Vector2d(1, -1));
	tiles[0][1] = Area->getTile(Position+Vector2d(-1, 0));
	tiles[1][1] = NULL;
	tiles[2][1] = Area->getTile(Position+Vector2d(1, 0));
	tiles[0][2] = Area->getTile(Position+Vector2d(-1, 1));
	tiles[1][2] = Area->getTile(Position+Vector2d(0, 1));
	tiles[2][2] = Area->getTile(Position+Vector2d(1, 1));

	//for (unsigned int x = 0; x < 3; ++x)
	//	for (unsigned int y = 0; y < 3; ++y)
	//		if (tiles[x][y] && tiles[x][y]->getTileType() == ETT_OPENABLE)
	//		{
	//			((OpenableTile*)tiles[x][y])->toggle();
	//			return Databank::Race[Race].Speed;
	//		}

	return 0;
}
